<template>
    <div>
        <Category :scene="scene"></Category>
        <!-- 已有的spu数据 -->
        <el-card shadow="always" :body-style="{ padding: '20px' }">
            <div v-show="scene == 0">
                <el-button type="primary" size="default" :icon="Plus" :disabled="!categoryStore.c3Id" @click="addSpu">
                    添加SPU</el-button>
                <el-table border stripe style="margin:20px 0px" :data="spuList.records" @sort-change="sortChange">
                    <el-table-column label="序号" width="100px"></el-table-column>
                    <el-table-column label="SPU名称" prop="spuName" show-overflow-tooltip resizable sortable>
                    </el-table-column>
                    <el-table-column label="SPU描述" prop="description" show-overflow-tooltip resizable></el-table-column>
                    <el-table-column label="操作">
                        <template v-slot="{row,$index}">
                            <el-button type="primary" size="small" title="添加SKU" :icon="Plus" @click="addSku(row)">
                            </el-button>
                            <el-button type="warning" size="small" title="修改SPU" :icon="Edit" @click="updateSpu(row)">
                            </el-button>
                            <el-button type="info" size="small" title="查看SKU" :icon="InfoFilled"
                                @click="showDialog(row)"></el-button>
                            <el-button type="danger" size="small" title="删除SPU" :icon="Delete"></el-button>
                        </template>
                    </el-table-column>
                </el-table>
                <!-- 分页器 -->
                <el-pagination v-model:current-page="currentPage" :page-sizes="[3,5,7]" v-model:page-size="pageSize"
                    @current-change="getSpuList" @size-change="sizeHandler" :total="spuList.total" background
                    layout="prev, pager, next, jumper,->,sizes,total" prev-text="上一页" next-text="下一页" :pager-count="7">
                </el-pagination>
            </div>
            <Sku v-show="scene ==2" @changeScene="changeSceneHandler"></Sku>
            <Spu v-show="scene ==1" @changeScene="changeSceneHandler" ref="spuRef"></Spu>

            <!-- 展示SKU的弹窗 -->
            <el-dialog v-model="dialogTableVisible" stripe title="查看SKU列表">
                <el-table >
                    <el-table-column label="名称" width="150"></el-table-column>
                    <el-table-column label="价格" width="200"></el-table-column>
                    <el-table-column label="重量"></el-table-column>
                    <el-table-column label="默认图片">
                        <template #="{row,$index}">
                            <img src="" alt="" style="width:100;height:100">
                        </template>
                    </el-table-column>
                </el-table>

            </el-dialog>

        </el-card>

    </div>
</template>

<script setup lang="ts">
import { Delete, Edit, Plus, InfoFilled } from "@element-plus/icons-vue";

import { ref, reactive, watch } from 'vue'

import { reqSpuList,reqFindSkuList } from '@/api/product/spu/spu'
//引入仓库
import useCategoryStore from "@/stores/category";
let categoryStore = useCategoryStore();
//引入子组件
import Spu from './addOrUpdateSpu.vue'
import Sku from './addSku.vue'

//场景值
let scene = ref<number>(0)
//分页器数据
let currentPage = ref<number>(1)
let pageSize = ref<number>(3)
//存储子组件实例spu
let spuRef = ref<any>()
//存储子组件实例sku
let skuRef = ref<any>();
//响应式spu数据
let spuList = ref({})
//控制弹窗的显示与隐藏
let dialogTableVisible = ref<boolean>(false);
//SKU列表
let skuList = ref([])


//监听三级分类id变化
watch(
    () => categoryStore.c3Id,
    () => {
        if (categoryStore.c3Id == '') return
        getSpuList();
    }
);

//获取spu数据
const getSpuList = async (pager: number = 1) => {
    //整理参数
    currentPage.value = pager;
    //返回的SPU列表数据
    let result = await reqSpuList(currentPage.value, pageSize.value, categoryStore.c3Id);
    //存储服务器数据
    spuList.value = result;
};

//分页器展示数据个数变化
const sizeHandler = (limit: number) => {
    pageSize.value = limit;
    //获取最新数据
    getSpuList();
}

//添加Spu按钮的回调
const addSpu = () => {
    scene.value = 1;
}

//修改spu按钮的回调
const updateSpu = (hasSpu) => {
    scene.value = 1;
    //获取子组件实例:调用子组件的方法
    spuRef.value.initHasSpuData(hasSpu);
}
//子组件SPU自定义事件回调
const changeSceneHandler = (params: number) => {
    //修改场景
    scene.value = params;
}

//添加sku按钮的回调
const addSku = (spu) => {
    scene.value = 2;
    //获取子组件实例，调用子组件方法
    skuRef.value.initSku(categortStore.c1Id, categortStore.c2Id, spu);
}

//展示dialog的回调
const showDialog = async(spu) => {
    //显示对话框
    dialogTableVisible.value = true;
    //获取sku列表
    let skus = await reqFindSkuList(spu.id)
    skuList.value = skus
    console.log(skuList)
}


</script>

<style scoped>

</style>